import { createStore } from 'vuex'
import per from 'vuex-persistedstate'

export default createStore({
  plugins: [
    per()
  ],
  state: {
    addressList: []
  },
  getters: {
  },
  mutations: {
    // 添加地址
    add(state, obj) {
      state.addressList.push(obj)
    },
    // 设置默认
    setDefault(state, id) {
      state.addressList.forEach(item => {
        item.isDefault = false
        if (item.id === id) {
          item.isDefault = true;
        }
      })
    },
    // 删除地址
    del(state, id) {
      state.addressList = state.addressList.filter(item => item.id != id)
    },
    // 修改
    edit(state, obj) {
      let index = state.addressList.findIndex(item => item.id == obj.id)
      state.addressList[index] = obj
    }
  },
  actions: {
  },
  modules: {
  }
})
